﻿using System.Web.Mvc;
using AmazeUI.bll;
using AmazeUI.bll.Open;
using AmazeUI.db;
using Ema.Filters;
using Ema.Models.XtModel;
using Newtonsoft.Json;

namespace Ema.Controllers
{
    /// <summary>
    /// The admin controller.
    /// </summary>
    public class AdminController : BaseController
    {
        /// <summary>
        /// The xt user bll.
        /// </summary>
        private XtUserBll xtUserBll = new XtUserBll();

        /// <summary>
        /// 菜单
        /// </summary>
        private XtNasBll xtNasBll = new XtNasBll();
        /// <summary>
        /// 角色
        /// </summary>
        private XtRoleBll roleBll = new XtRoleBll();
        /// <summary>
        /// 日志
        /// </summary>
        private XtLogBll xtLogBll = new XtLogBll();
        /// <summary>
        /// 后台首页
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        [AdminValidate]
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="url">
        /// url指向
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Login(string url = "")
        {
            ViewBag.url = url;
            return this.View("Login");
        }

        /// <summary>
        /// The 登录
        /// </summary>
        /// <param name="model">
        /// sysuser模型
        /// </param>
        /// <param name="url">
        /// 返回url
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        [HttpPost]
        public ActionResult Login(SubXtUser model, string url = "")
        {
            XtUser login = xtUserBll.Login(model.UserName, model.Pwd);

            //是否登录成功
            if (login != null)
            {
                xtUserBll.LoginEna(login.Id, GetIp);
                this.GetAdmin = login;

                //写入登录信息
                base.Logset("admin.login", "后台登录");
                if (string.IsNullOrWhiteSpace(url))
                {
                    return this.RedirectToAction("Index", "Admin");
                }

                return this.Redirect(url);
            }

            this.ViewBag.ms = "账号或密码错误";
            this.ViewBag.url = url;
            return this.View();
        }

        /// <summary>
        /// 登录信息
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult DlView()
        {
            var loninModel = this.GetAdmin;
            return this.PartialView(loninModel);
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Logout()
        {
            this.GetAdmin = null;
            return RedirectToAction("Login", "Admin");
        }

        /// <summary>
        /// 退出登录View
        /// </summary>
        /// <returns></returns>
        public ActionResult LogoutView()
        {

            return this.PartialView(this.GetAdmin);
        }

        /// <summary>
        /// 权限不足页面
        /// </summary>
        /// <param name="url">
        /// The url.
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult NotRole(string url)
        {
            return this.View();
        }

        /// <summary>
        /// 管理员信息
        /// </summary>
        /// <returns></returns>
        [LoginValidate]
        public ActionResult Info()
        {
            if (this.GetAdmin != null)
            {
                return this.Content(this.GetAdmin.Name);
            }
            else
            {
                return this.Content("未登录");
            }
        }


        /// <summary>
        /// 管理左侧栏
        /// </summary>
        /// <returns></returns>
        public ActionResult NasList()
        {
            return this.View();
        }
        /// <summary>
        /// 管理左侧栏
        /// </summary>
        /// <returns></returns>
        public ActionResult DataNasList(int rows, int page)
        {
            var list = this.xtNasBll.GetList(rows, page);
            return this.GetJsonDb(list);
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        public ActionResult Addrole()
        {
            ViewBag.RoleId = roleBll.GetXtPowersJsonList();
            return this.View();
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Addrole(SubXtRole model)
        {
            if (this.ModelState.IsValid)
            {
                roleBll.AddXtRole(model);
                return RedirectToAction("RoleList");
            }
            ViewBag.Powers = roleBll.GetXtPowersJsonList();
            return this.View();
        }
        /// <summary>
        /// 权限列表
        /// </summary>
        /// <returns></returns>
        [JichengValidate]
        public ActionResult RoleList()
        {
            ViewBag.Powers = roleBll.GetXtPowersJsonList();
            return this.View();
        }

        /// <summary>
        /// 权限列表data
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DataRoleList(int rows, int page)
        {
            var r = this.roleBll.List(rows, page);
            return this.GetJsonDb(r);
        }


        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [LoginValidate]
        public ActionResult RoleEdit(string json)
        {
            var subXtRole = JsonConvert.DeserializeObject<SubXtRole>(json);
            var b = this.roleBll.Edit(subXtRole);
            return this.GetJsonDb(b);
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        /// <returns></returns>

        [LoginValidate]
        public ActionResult XtLog()
        {
            return this.View();
        }

        /// <summary>
        /// 获取系统日志
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DataXtLog(int rows, int page)
        {
            var xlp = xtLogBll.GetPage(rows, page);
            return this.GetJsonDb(xlp);
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        /// <returns></returns>

        [LoginValidate]
        public ActionResult XtError()
        {
            return this.View();
        }

        /// <summary>
        /// 获取系统日志
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpPost]
        [LoginValidate]
        public ActionResult DataXtError(int rows, int page)
        {
            var model = xtLogBll.GetErrorPage(rows, page);
            return this.GetJsonDb(model);
        }

        /// <summary>
        /// 系统订单页
        /// </summary> 
        /// <returns></returns> 
        [AdminValidate]
        public ActionResult PayOrder()
        {
            return this.View();
        }

        /// <summary>
        /// 获取订单数据
        /// </summary>
        /// <returns></returns>
        [AdminValidate]
        [HttpPost]
        public ActionResult DataPayOrderlist(int rows, int page, string wd)
        {
            PayBll pay = new PayBll();
            var ps = pay.PayOrderQuery(rows, page, wd);
            return this.GetJsonDb(ps);
        }
        /// <summary>
        /// 后台订单付款
        /// </summary>
        /// <returns></returns>
        [AdminValidate]
        [HttpPost]
        public ActionResult GoPay(int id)
        {
            throw new System.NotImplementedException();
        }
    }
}
